package com.dengc;

import java.awt.*;

public class Line {
    //索道的起点坐标
    int x = 380;
    int y = 180;
    //终点坐标
    int endx = 500;
    int endy = 500;
    //根据公式计算线的摆动
    double length = 100;
    //min
    double MIN_length = 100;
    //max
    double MAX_length = 750;

    double n = 0;
    //方向
    int dir = 1;
    //状态 0为摇摆，1为抓取,2返回，3抓到东西
    int status;
    //爪子图片
    Image hook = Toolkit.getDefaultToolkit().getImage("imgs/hook.png");

    //获取主窗口属性
    GameWin frame;
    Line(GameWin frame){this.frame = frame;}
    //判断抓取方法,判断是否碰撞到东西
    void logic(){
        for (Object obj: this.frame.objectList) {
            if ( endx > obj.x && endx < obj.x + obj.width
                    && endy > obj.y && endy < obj.y + obj.height){
                status = 3;
                obj.flag = true;
            }
        }
    }

    //绘制方法
    void lines(Graphics g){
        endx = (int) (x + length * Math.cos(n * Math.PI));
        endy = (int) (y + length * Math.sin(n * Math.PI));
        g.setColor(Color.green);
        g.drawLine(x-1,y-1,endx,endy);
        g.drawLine(x,y,endx,endy);
        g.drawLine(x+1,y+1,endx,endy);
        g.drawImage(hook,endx-36,endy-2,null);
    }

    void paintSelf(Graphics g) throws InterruptedException {
        logic();
        switch (status){
            case 0:
                if (n < 0.1){
                    dir = 1;
                }else if (n > 0.9){
                    dir = -1;
                }
                n += 0.005 * dir;
                lines(g);
                break;
            case 1:
                if (length<MAX_length){
                    length += 10;
                    lines(g);
                }else {
                    status = 2;
                }
                break;
            case 2:
                if (length>MIN_length){
                    length -= 10;
                    lines(g);
                } else {
                    //收回后返回摇摆状态
                    status = 0;
                }
                break;
            case 3:
                int m = 1;
                if (length > MIN_length){
                    length -= 5;
                    lines(g);
                    for (Object obj : this.frame.objectList){
                        if (obj.flag){
                            m = obj.m;
                            obj.x = endx - obj.getWidth()/2;
                            obj.y = endy;
                            if (length <= 100){
                                //抓取成功后移出金块
                                obj.x = -150;
                                obj.y = -150;
                                obj.flag = false;
                                //加分
                                Bg.count += obj.count;
                                //完成后取消掉药水状态
                                Bg.waterFlag = false;
                                //收回后返回摇摆状态
                                status = 0;
                            }
                            if (Bg.waterFlag){
                                if (obj.type == 1){
                                    //进行快速抓取
                                    m = 1;
                                }
                                if (obj.type == 2){
                                    //抓到石头直接爆破
                                    obj.x = -150;
                                    obj.y = -150;
                                    obj.flag = false;
                                    //完成后取消掉药水状态
                                    Bg.waterFlag = false;
                                    //收回后返回摇摆状态
                                    status = 2;
                                }
                            }
                        }
                    }
                }
                Thread.sleep(m);
                break;
            default:
    }
    }
    //重置元素
    void  reGame(){
        n = 0;
        length = 100;
    }
}
